<?php


namespace app\zhongcan\controller;


use app\zhongcan\model\NoticeModel;
use app\zhongcan\validate\NoticeValidate;
use cmf\controller\AdminBaseController;
use think\Request;

class NoticeController extends AdminBaseController
{
    public function index(Request $request)
    {
        $selkey = $request->get('selkey');
        $where = [];
        if($selkey)
        {
            $where[]=['title','like','%'.$selkey.'%'];
        }
        $res = NoticeModel::order('list_order','desc')
            ->where($where)
            ->paginate(2);
        $this->assign('list',$res);
        return $this->fetch();
    }
    public function add()
    {

        return $this->fetch();
    }
    public function addPost(Request $request)
    {
        $data = $request->param();
        //富文本处理
        $content = htmlspecialchars_decode($data['content']);
        $content = cmf_replace_content_file_url($content,'true');
        $data['content'] = htmlspecialchars($content);
        //图片处理
        $data['image'] = cmf_get_image_url($data['image']);
        //创建时间
        $create_time = date("Y-m-d H-i-s");
        //验证器
        $validate = new NoticeValidate();
        $val = $validate->scene('add')->check($data);
        if(!$val)
        {
            $this->error($validate->getError());
        }
        $res = NoticeModel::create([
            'title'=>$data['title'],
            'author'=>$data['author'],
            'look_num'=>$data['look_num'],
            'abstract'=>$data['abstract'],
            'content'=>$data['content'],
            'create_time'=>$create_time,
            'image'=>$data['image']
        ]);
        if($res){
             $this->success('添加成功');
        }else{
             $this->error('添加失败');
        }
    }
    //修改页面
    public function edit(Request $request)
    {
        $id = $request->param('id');
        $res = NoticeModel::where('id','=',$id)
            ->find();
        //富文本处理
        $content = htmlspecialchars_decode($res['content']);
        $content = cmf_replace_content_file_url($content);
        $this->assign('res',$res);
        return $this->fetch('edit',[
            'res'=>$res,
            'content'=>$content
        ]);
    }
    //修改提交
    public function editPost(Request $request)
    {
        $data = $request->param();
        //富文本处理
        $content = htmlspecialchars_decode($data['content']);
        $content = cmf_replace_content_file_url($content,'true');
        $data['content'] = htmlspecialchars($content);
        //图片处理
        $data['image'] = cmf_get_image_url($data['image']);
        $res = NoticeModel::where('id','=',$data['id'])
            ->update([
                'title'=>$data['title'],
                'author'=>$data['author'],
                'content'=>$data['content'],
                'image'=>$data['image'],
                'update_time'=>date("Y:m:d H:i:s")
            ]);
        if($res){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }
    //是否展示
    public function show(Request $request)
    {
        $id = $request->param('id/d');
        $res = NoticeModel::where('id',$id)->find();

        if($res['switch'] == 1){
            $res['switch'] = 0;
        }else{
            $res['switch'] = 1;
        }
        $list = NoticeModel::where('id','=',$id)
            ->update([
            'switch'=>$res['switch']
        ]);
        if($list){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    //删除
    public function delete(Request $request)
    {
        $id = $request->param('id/d');
        $res = NoticeModel::where('id',$id)
            ->delete();
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    //排序
    public function order()
    {
        parent::listOrders(new NoticeModel());
        $this->success('排序成功');
    }
    //内容查看
    public function content(Request $request)
    {
        $id =$request->param('id');
        $res = NoticeModel::where('id',$id)
            ->find();
        $content = htmlspecialchars_decode($res['content']);
        $res['content'] = cmf_replace_content_file_url($content);
        $this->assign('res',$res);
        return $this->fetch();
    }
}